//
//  DMWelcomeView.swift
//  DM_新浪微博
//
//  Created by Damon.JiAo on 16/2/29.
//  Copyright © 2016年 Demon_JiAo. All rights reserved.
//

import UIKit
import SDWebImage


class DMWelcomeView: UIViewController {

    override func loadView() {
        view = bgImageView
    }
    
    override func viewDidLoad() {
        setInfo()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        startAnimation()
    }
    
    /** 动画方法 */
    private func startAnimation(){
        
        // 更新约束
        photoImageView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(-UIScreen.mainScreen().bounds.height + 200)
            
            // 其他的约束不要修改.,直接复制过来
            make.centerX.equalTo(self.view.snp_centerX)
            
            //设置一个宽高
            make.size.equalTo(CGSizeMake(170/2, 170/2))
        }
        
        UIView.animateWithDuration(2.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            
            /** 更新约束需要调用此方法 */
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
            NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)
        }
        
    }
    
    /** 添加一个增加视图、添加视图约束-的方法 */
    private func setInfo(){
        
        view.addSubview(photoImageView)
        view.addSubview(iconLabel)
        
        //  设置约束
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-150)
            make.centerX.equalTo(view.snp_centerX)
            
            make.size.equalTo(CGSizeMake(170/2, 170/2))
        }
        iconLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(photoImageView.snp_bottom).offset(10)
            make.centerX.equalTo(view.snp_centerX)
        }
        
        //头像修改 -- placeholder
        // public convenience init?(string URLString: String)
        let url = NSURL(string: DMUserViewModel.shareInstance.model?.profile_image_url ?? "")
        if let Ourl = url{
            
            photoImageView.sd_setImageWithURL(Ourl, placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
    }
    
    /** 背景图片 */
    private lazy var bgImageView:UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "ad_background")
        
        return img
        
    }()
    
    /** 头像 */
    private lazy var photoImageView:UIImageView = {
       
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_default_big")
        
        img.layer.cornerRadius = 170 / 4
        // 裁剪多余的
        img.layer.masksToBounds = true
        img.layer.borderColor = UIColor.grayColor().CGColor
        
        img.layer.borderWidth = 2
        
        return img
    }()
    
    /** Label */
    private lazy var iconLabel:UILabel = {
        
        let label = UILabel()
        
        label.textAlignment = .Center
        
        label.font = UIFont.systemFontOfSize(15)
        
        label.textColor = UIColor.darkGrayColor()
        
        label.text = "\(DMUserViewModel.shareInstance.model?.screen_name ?? ""),欢迎回来"
        
        return label
    }()
    
}
